// pages/index/slider/reset/phone/phone.js
import {
    Ble
} from "../../../../../utils/ble"
import {
    Protocol
} from "../../../../../utils/protocol"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        img_Phone1: '/image/icon/reset_phone_1.png',
        img_Phone2: '/image/icon/reset_phone_2.png',
        img_Phone3: '/image/icon/reset_phone_3.png',
        bleStatus: "",
        macAddress: 0x00FFEECC,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //   蓝牙通信
    sendData(bytes) {
        Ble.server.startAdvertising({
            advertiseRequest: {
                serviceUuids: bytes
            },
            powerLevel: 'high',
            success: (res) => {
                console.log(" advertise message success ")
                this.timer = setTimeout(() => {
                    Ble.server.stopAdvertising({})
                }, 300)
            },
            fail: (error) => {
                console.log(JSON.stringify(error))
            },
            complete: () => {
                console.log(" advertise message complete ")
            }
        })
    },
    reset(e) {
        let params = new Uint8Array(9)
        params[0] = 0x00
        let bytes = Protocol.encode(0x03, this.data.macAddress, params)
        this.sendData(bytes)
    },
})